// 初始化菜单的数量
// 一级菜单
firstMenuCount = 0;
// 二级菜单数量              
secondMenuCount = [0, 0, 0, 0];
// 一级菜单最大的个数
firstMenuMaxCount = 0;
// 二级菜单最大个数
secondMenuMaxCount = 0;
// 上一个获取焦点的空间
selectMenu = null;
// 记录根路由
serverRoot = null;

$.fn.wechart = function (options) {
    // 默认值
    var defaults = {
        'title': 'test',
        'firstMenuMaxCount': 4,
        'secondMenuMaxCount': 5,
        'serverRoot': '/wechat/',
    };
    var settings = $.extend(defaults, options);
    // 获取配置中的标题
    var title = settings.title;
    // 获取根路由
    serverRoot = settings.serverRoot;
    // 初始化菜单的数量
    // 一级菜单最大的个数
    firstMenuMaxCount = settings.firstMenuMaxCount;
    // 二级菜单最大个数
    secondMenuMaxCount = settings.secondMenuMaxCount;
    // 初始化预览的宽和高
    $(this).css('width', '317px');
    $(this).css('height', '540px');
    
    // 增加预览的视图
    $(this).append( '<div class="pre-menu-title">' + title + '</div>' +
                    '<div class="pre-menu-bd">' +
                        '<ul class="pre-menu-list" id="firstMenuList">' +
                            // '<li class="pre-menu-item current sizelof2">' +
                            //     '<a class="pre-menu-link" href="javascript:void(0)" >' +
                            //         '111111' +
                            //     '</a>' +
                            //     '<div class="sub-pre-menu">' +
                            //         '<ul class="sub-pre-menu-list">' +
                            //             '<li class="sub-pre-menu-item">' +
                            //                 '<a href="javascript:void(0)" onclick="addSecondMenu(this, firstMenuIndex)" class="sub-pre-menu-link">' +
                            //                     '<i class="fa fa-plus menu-add" aria-hidden="true"></i>' +
                            //                 '</a>' +
                            //             '</li>' +
                            //         '</ul>' +
                            //     '</div>' +
                            // '</li>' +
                            // '<li class="pre-menu-item sizelof2">' +
                            //     '<a class="pre-menu-link" onclick="addFirstMenu(this)" href="javascript:void(0)" title="2222">' +
                            //         '<i class="fa fa-plus menu-add" aria-hidden="true"></i>' +
                            //     '</a>' +
                            // '</li>' +
                        '</ul>' +
                    '</div>');
    // 初始化预览图
    getMenuData();

    // 添加二级菜单
    function addSecondMenu(obj, firstMenuIndex) {

    }

    // 获取菜单数据
    function getMenuData() {
        var url = serverRoot + 'menu/list/';
        $.ajax({
            url: url,
            type: 'GET',
            dataType: 'JSON',
            success: function(ret) {
                if(ret.code == 200) {
                    var menuData = ret.menu;
                    initMenuData(menuData);
                }
            }
        });
    }

    // 初始化菜单
    function initMenuData(menuData) {
        var menuData = menuData;

        var firstMenu = menuData.button;
        // 初始化一级菜单的长度
        firstMenuCount = firstMenu.length;
        // 判断后面是否在加一个菜单
        var firstMenuTotalCount = 0;
        if(firstMenuCount < firstMenuMaxCount) {
            firstMenuTotalCount = firstMenuCount + 1;
        }else {
            firstMenuTotalCount = firstMenuCount;
        }
        for(i = 0; i < firstMenu.length; i++) {
            // 增加一级菜单
            $('#firstMenuList').append('<li data-id="menu_' + firstMenu[i].id + '" class="pre-menu-item sizelof' + firstMenuTotalCount + '">' + 
                                            '<a data-id="' + firstMenu[i].id + '" class="pre-menu-link" onclick="getSelectMenuData(this)" href="javascript:void(0)" >' + 
                                                firstMenu[i].name + 
                                            '</a>' + 
                                        '</li>');
            // 二级菜单的长度
            var secondMenuCountTemp = 0;
            if(firstMenu[i].sub_button != undefined) {
                // 增加二级菜单
                var secondMenu = firstMenu[i].sub_button;
                // 记录二级菜单的长度
                secondMenuCount[i] = secondMenu.length;
                // 添加二级菜单到
                $('li[data-id="menu_' + firstMenu[i].id + '"]').append( '<div class="sub-pre-menu">' +
                                                                            '<ul data-id="subMenuList_'+ firstMenu[i].id +'" class="sub-pre-menu-list">' +
                                                                                
                                                                            '</ul>' +
                                                                        '</div>');
                // 添加二级菜单
                var secondMenuListRoot =  $('ul[data-id="subMenuList_' + firstMenu[i].id + '"]');
                for(j=0; j<secondMenu.length;j++) {
                    secondMenuListRoot.append(  '<li class="sub-pre-menu-item">' +
                                                    '<a data-id="' + secondMenu[j].id + '" href="javascript:void(0)" onclick="getSelectMenuData(this)" class="sub-pre-menu-link">' +
                                                        secondMenu[j].name + 
                                                    '</a>' +
                                                '</li>');
                }
                // 判断二级菜单的个数是否为
                if(secondMenuCount[i] < secondMenuMaxCount) {
                    // 在最后增加一个添加符号
                    secondMenuListRoot.append( '<li class="sub-pre-menu-item">' +
                                                    '<a data-parent-id="'+ firstMenu[i].id +'" href="javascript:void(0)" onclick="addSecondMenu(this, ' + i + ')" class="sub-pre-menu-link">' +
                                                        '<i class="fa fa-plus menu-add" aria-hidden="true"></i>' +
                                                    '</a>' +
                                                '</li>');
                }
            } else {
                // 添加二级菜单到
                $('li[data-id="menu_' + firstMenu[i].id + '"]').append( '<div class="sub-pre-menu">' +
                                                                            '<ul class="sub-pre-menu-list">' +
                                                                                    '<li class="sub-pre-menu-item">' +
                                                                                    '<a data-parent-id="'+ firstMenu[i].id +'" href="javascript:void(0)" onclick="addSecondMenu(this, ' + i + ')" class="sub-pre-menu-link">' +
                                                                                        '<i class="fa fa-plus menu-add" aria-hidden="true"></i>' +
                                                                                    '</a>' +
                                                                                '</li>' +
                                                                            '</ul>' +
                                                                        '</div>');
            }
        }
        // 在一级菜单末尾添加加号
        if(firstMenuCount < firstMenuMaxCount) {
            $('#firstMenuList').append('<li class="pre-menu-item sizelof'+firstMenuTotalCount+'">' +
                                            '<a class="pre-menu-link" onclick="addFirstMenu(this, '+ firstMenuCount +')" href="javascript:void(0)">' +
                                                '<i class="fa fa-plus menu-add" aria-hidden="true"></i>' +
                                            '</a>' +
                                        '</li>');
        }

        // console.log(secondMenuCount);
    }
}

// 添加一级菜单
function addFirstMenu(obj, firstMenuIndex) {

    var url = serverRoot + 'menu/item/create/';

    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'JSON',
        data: {
            mtype: 2,
            mindex: firstMenuIndex,
            name: '新建菜单',
            level: 1,
        },
        success: function(ret) {
            if(ret.code == 200) {
                var menu = ret.menu;
                $(obj).children('i').remove();
                $(obj).append(menu.name);
                $(obj).attr('data-id', menu.id);
                $(obj).addClass('current');
                $(obj).attr('onclick', 'getSelectMenuData(this)');
                firstMenuCount = firstMenuCount + 1;
                if(firstMenuCount < firstMenuMaxCount) {
                    firstMenuTotalCount = firstMenuCount + 1;
                    $('#firstMenuList').append('<li class="pre-menu-item sizelof'+firstMenuTotalCount+'">' +
                                                    '<a class="pre-menu-link" onclick="addFirstMenu(this, ' + firstMenuCount + ')" href="javascript:void(0)">' +
                                                        '<i class="fa fa-plus menu-add" aria-hidden="true"></i>' +
                                                    '</a>' +
                                                '</li>');
                    $('#firstMenuList').children('li').removeClass('sizelof' + firstMenuCount);
                    $('#firstMenuList').children('li').addClass('sizelof' + firstMenuTotalCount);
                }
            
                // 在这个一级菜单上添加二级菜单的添加按钮
                $(obj).parent().append(  '<div class="sub-pre-menu">' +
                                            '<ul class="sub-pre-menu-list">' +
                                                '<li class="sub-pre-menu-item">' +
                                                    '<a data-parent-id="'+ menu.id +'" href="javascript:void(0)" onclick="addSecondMenu(this, ' + firstMenuIndex + ')" class="sub-pre-menu-link">' +
                                                        '<i class="fa fa-plus menu-add" aria-hidden="true"></i>' +
                                                    '</a>' +
                                                '</li>' +
                                            '</ul>' +
                                        '</div>');
            }
        }
    });
}

// 添加二级菜单
function addSecondMenu(obj, firstMenuIndex) {
    // 获取当前二级菜单的个数
    var currentCount = secondMenuCount[firstMenuIndex];
    // 获取当前二级菜单列表的根
    var secondMenuListRoot = $(obj).parent().parent();
    var url = serverRoot + 'menu/item/create/';
    var parentId = $(obj).attr('data-parent-id');

    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'JSON',
        data: {
            mtype: 2,
            mindex: currentCount,
            name: '新建菜单',
            level: 2,
            parent_id: parentId
        },
        success: function(ret) {
            if(ret.code == 200) {
                var menu = ret.menu;
                $(obj).children('i').remove();
                $(obj).append(menu.name);
                $(obj).addClass('current');
                $(obj).attr('onclick', 'getSelectMenuData(this)');
                $(obj).attr('data-id', menu.id)
            
                var totalCount = currentCount + 1;
                if(totalCount < secondMenuMaxCount) {
                    
                    secondMenuCount[firstMenuIndex] = totalCount;
                    secondMenuListRoot.append( '<li class="sub-pre-menu-item">' +
                                                    '<a data-parent-id="'+menu.parent_id+'" href="javascript:void(0)" onclick="addSecondMenu(this, ' + firstMenuIndex + ')" class="sub-pre-menu-link">' +
                                                        '<i class="fa fa-plus menu-add" aria-hidden="true"></i>' +
                                                    '</a>' +
                                                '</li>');
                }
            }
        }
    });

    
    // console.log(secondMenuCount);
}

// 获取菜单信息
function getSelectMenuData(obj) {
    if(selectMenu != null && selectMenu.length > 0) {
        selectMenu.parent().removeClass('current');
    }
    $(obj).parent().addClass('current');
    selectMenu = $(obj);
}
